package dao;

import java.sql.*;

import utilClasses.ListaTipVozila;
import utilClasses.ListaVozila;
import utilClasses.uVozilo;
import utilClasses.uTipVozila;





public class daoVozilo {
	
	
	
	
	public static void unesiVozilo( String tipVozila, double gsirina, double gduzina, String nazivLinije) throws SQLException 
	{
		Connection c =  daoKonekcija.dajKonekciju();
		try {
			CallableStatement cStmt1 = c.prepareCall("{CALL dajIdVozila(?)}"); 
			cStmt1.setString(1,tipVozila);
			ResultSet rs1 = cStmt1.executeQuery(); ;
			int idv=0;
			if(rs1.next()) {
				idv=rs1.getInt(1);
			}
			rs1.close();
			cStmt1.close();

CallableStatement cStmt2 =  c.prepareCall("{CALL dajIdLinije (?)}"); 
			cStmt2.setString(1,nazivLinije);
			ResultSet rs2 = cStmt2.executeQuery();
			int idl=0;
			if(rs2.next()) {
				idl=rs2.getInt(1);
			}
			rs2.close();
			cStmt2.close();
			
			CallableStatement cStmt3 =  c.prepareCall("{CALL Insert_vozilo(?,?,?,?)}");
			cStmt3.setInt(1, idv);
			cStmt3.setDouble(2, gsirina);
			cStmt3.setDouble(3, gduzina);
			cStmt3.setInt(4, idl);
			System.out.println(idv);
			cStmt3.executeUpdate();
			
			System.out.println("Uspjesno uneseno vozilo!\n");
			cStmt3.close();
			c.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public static ListaTipVozila dajImenaVozila() throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
		ListaTipVozila rez=new ListaTipVozila();
		String iskaz = "SELECT nazivVozila FROM tipvozila";
		try {
			Statement stmt =  c.createStatement();
			ResultSet rs=stmt.executeQuery(iskaz);
			while (rs.next()) {
		    	String naziv=rs.getString(1);
		        rez.getTipVozila().add(new uTipVozila(naziv)); 
		        }
			rs.close();
		    stmt.close();
		    return rez;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	
	

	
	public static String dajImeTipaVozila(int idVozila) throws SQLException{
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt3;
		String ime=null;
		try {
			cStmt3 =  c.prepareCall("{CALL dajImeTipVozila (?)}");
		
		
		cStmt3.setInt(1, idVozila);
    	ResultSet rs1 = cStmt3.executeQuery(); ;
		
		if(rs1.next()) {
			ime=rs1.getString(1);
		}
		rs1.close();
		cStmt3.close();
		c.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ime;
	}
	
	
	public static String dajImeLinije(int idLinije) throws SQLException{
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt3;
		String ime=null;
		try {
			cStmt3 =  c.prepareCall("{CALL dajImeLinije (?)}");
		
		
		cStmt3.setInt(1, idLinije);
    	ResultSet rs1 = cStmt3.executeQuery(); ;
		
		if(rs1.next()) {
			ime=rs1.getString(1);
		}
		rs1.close();
		cStmt3.close();
		c.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ime;
	}
	
	
	public static ListaVozila dajVozila() throws SQLException
	{
		Connection c = daoKonekcija.dajKonekciju();
		ListaVozila rez=new ListaVozila();
		String iskaz = "SELECT * FROM vozilo";
		try {
			Statement stmt =  c.createStatement();
			ResultSet rs=stmt.executeQuery(iskaz);
			while (rs.next()) {
				int idVozila=rs.getInt("idVozilo");
		    	int idTipVozila=rs.getInt("idTipVozila");
		    	double gsirina=rs.getDouble("GSirina");
		    	double gduzina= rs.getDouble("GDuzina");
		    	int idLinija=rs.getInt("idLinija");
		    	String imeTipaVozila= dajImeTipaVozila(idTipVozila);
		    	String imeLinije= dajImeLinije(idLinija);
		        rez.getVozila().add(new uVozilo(idVozila,imeTipaVozila,gsirina,gduzina,imeLinije)); 
		        }
		    
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(c!=null)c.close();
		return rez;
		}
	
	public static void brisiVozilo(String id) throws Exception
	{
		Connection c = daoKonekcija.dajKonekciju();
		int idVozila= Integer.parseInt(id);
		CallableStatement cStmt =  c.prepareCall("{CALL deleteVozilo(?)}"); 
		cStmt.setInt(1,idVozila);
		cStmt.execute(); 
		System.out.println("Uspjesno obrisano vozilo!"); 
		cStmt.close();
		c.close();
	}
}
	
